package com.camps.sdk.decrypt;

import com.camps.sdk.common.Constants;
import com.camps.sdk.entity.BatchWhiteCallReply;
import com.camps.sdk.entity.DeviceUpMessage;
import com.camps.sdk.entity.LocationData;
import com.camps.sdk.entity.WhiteCall;
import com.camps.sdk.utils.LocationUtils;
import com.camps.sdk.utils.UnicodeUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author ldaoliang
 * @Date create in 15:58 2021/9/1
 * @Description
 */
public class AnalyzeBatchWhiteCall implements AnalyticalData {
    @Override
    public DeviceUpMessage parseData(String originalData)
    {
        // *WT,866248050395985,V4,PBWLALL,141920,1,12487541256,1,0,\u0074\u0065\u0073\u0074,1,141922,V,0.0000,S,0.0000,W,0.00,0,030621,FFFDFFFF#
        //解密
        String[] split = parseCommand(originalData);
        int length=split.length;
        String imei=split[1];
        String commandNumber=split[4];
        int index=(length-15)/7;//总的长度减去指令中其他信息，算出有几组号码信息
        //存放所有的亲情号码信息
        List<Integer> sequenceList = new ArrayList<>();
        List<WhiteCall> whiteList=new ArrayList<>();
        for(int j=1;j<=index;j++){
            WhiteCall whiteCall=new WhiteCall();
            //假如是第一组 split[5]
            sequenceList.add(Integer.parseInt(split[4+(j-1)*7+1]));
            whiteCall.setSequence(Integer.parseInt(split[4+(j-1)*7+1]));
            whiteCall.setPhone(split[4+(j-1)*7+2]);
            whiteCall.setCommandNumber(commandNumber);
            String name = split[4+(j-1)*7+5];
            whiteCall.setName(name!=null && !name.equals("") ? UnicodeUtil.decodeUnicode(split[4+(j-1)*7+5]) : "" );
            whiteCall.setPictureId(Integer.parseInt(split[4+(j-1)*7+6]));
            whiteCall.setType(Integer.parseInt(split[4+(j-1)*7+3]));
            whiteList.add(whiteCall);
        }
        //获取所有的定位信息
        //时间时分秒
        String time=split[length-10];
        String lat = split[length-8];
        String lng = split[length-6];
        String speed = split[length-4];
        String orientation = split[length-3];
        //年月日
        String date=split[length-2];
        //设备状态
        String deviceStatus=split[length-1];
        List<Double> list = LocationUtils.transiformLatAndLng(lng, lat);
        BatchWhiteCallReply batchWhiteCallReply = new BatchWhiteCallReply();
        batchWhiteCallReply.setCommandNumber(commandNumber);
        batchWhiteCallReply.setDeviceDate(date);
        batchWhiteCallReply.setDeviceTime(time);
        batchWhiteCallReply.setImei(imei);
        batchWhiteCallReply.setList(whiteList);
        batchWhiteCallReply.setDeviceStatus(deviceStatus);
        LocationData LocationData = new LocationData();
        LocationData.setOrientation(Integer.parseInt(orientation));
        LocationData.setSpeed(Double.parseDouble(speed));
        LocationData.setLatitude(list.get(1));
        LocationData.setLongitude(list.get(0));
        batchWhiteCallReply.setLocationData(LocationData);
        return structureMessage(Constants.REPLY,Constants.CMD_BATCH_SET_WHITE_CALL,false,"",batchWhiteCallReply);
    }
}
